package com.common.handler;

import com.common.exception.DownloadFileNotFoundException;
import com.common.exception.FileStorageException;
import com.common.exception.InvalidFilePathException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@ControllerAdvice
public class GlobalExceptionHandler {
    //处理自定义异常
    @ExceptionHandler(FileStorageException.class)
    public ResponseEntity<String> handleFileStorageError(FileStorageException ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("文件存储错误: " + ex.getMessage());
    }
    //处理文件过大异常
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<String> handleSizeExceeded() {
        return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE)
                .body("文件过大，请上传小于 10MB 的文件");
    }
    @ExceptionHandler(DownloadFileNotFoundException.class)
    public ResponseEntity<String> handleFileNotFound(DownloadFileNotFoundException ex) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND)
               .body("文件未找到: " + ex.getMessage());
    }
}
